﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>参数设置列表页面</title>
    <link rel="stylesheet" type="text/css" href="../Content/Css/OperaMasksUI/default/om-all.css" />
    <link rel="stylesheet" type="text/css" href="../Content/Css/list.css" />
    <script type="text/javascript" src="../Scripts/jquery.1.6.3.min.js"></script>
    <script type="text/javascript" src="../Scripts/json2.js"></script>
    <script type="text/javascript" src="../Scripts/common.js"></script>
    <script type="text/javascript" src="../Scripts/OperaMasksUI/operamasks-ui.min.js"></script>
    <script type="text/javascript" src="../Scripts/Plugin/jquery.form.js"></script>
    <script type="text/javascript" src="../Scripts/OperaMasksUI/Plugin/om-yj-container.js"></script>
    <script type="text/javascript" src="../Scripts/Plugin/artDialog4.1.7/jquery.artDialog.js?skin=default"></script>
    <script type="text/javascript" src="../Scripts/Plugin/artDialog4.1.7/plugins/iframeTools.js"></script>
    <script type="text/javascript">
        var detailPanel, listGrid, colData, searchPanel, defaultSort, defaultWhere, validatorForm, submitForm, currentUID, currentGIndex, genRenderCheckbox, genRenderDateformat;
        var showSearchPanel, showToolBar, showDetialForm, controlBtn, oldRowData, detailForm, updateFlag = true, addFlag = false, goOn = true, ifh;
        var actionRoot = "../handler/ajax.ashx?type=AjaxSystem";

        $(document).ready(function () {
            $.ajax({
                cache: false, async: false,
                url: '../handler/ajax.ashx?type=AjaxSystem&method=loginIn',
                dataType: 'json', type: 'post',
                beforeSend: function () {
                    $.omMessageBox.waiting({
                        title: '请等待',
                        content: '服务器正在加载用户信息,请稍后...'
                    });
                },
                complete: function () {
                    $.omMessageBox.waiting("close");
                },
                success: function (data) {
                    if (data.IsError) {
                        goOn = false;
                        $.omMessageBox.alert({
                            content: '发现系统错误,请重新登录<BR>错误码：' + data.Message,
                            onClose: function () {
                                window.top.location.href = decodeURIComponent("Index.aspx");
                            }
                        });
                    }
                    else {
                        goOn = true;
                        currentUID = data.Data.userid
                    }
                },
                error: function (result) {
                    goOn = false;
                    $.omMessageBox.alert({
                        content: '发现系统错误,请重新登录<BR>错误码：' + result.status,
                        onClose: function () {
                            window.top.location.href = decodeURIComponent("Index.aspx");
                        }
                    });
                }
            });

            var element = $('body').omBorderLayout({
                fit: true,
                spacing: 0,
                panels: [
                {
                    id: "center-panel",
                    header: false,
                    title: "中间模块",
                    region: "center"
                }, {
                    id: "west-panel",
                    resizable: true,
                    collapsible: true,
                    title: '查询条件',
                    region: "west",
                    width: 280
                },
                {
                    id: "south-panel",
                    region: "south",
                    header: false,
                    resizable: false,
                    collapsible: false,
                    height: 35
                }
                ]
            });

            $('#container1').omContainer({ fit: true });
            detailForm = $('#viewForm');
            showSearchPanel();
            showToolBar(detailForm);
            $("#DateValue", detailForm).omCalendar();
            controlBtn(detailForm, 'enable', 'enable', 'disable', 'disable', 'disable');
            $(":input", detailForm).change(function () { controlBtn(detailForm, 'disable', 'disable', 'disable', 'enable', 'enable'); detailForm.data('changed', true); });
            validatorForm = $('#viewForm').validate({
                rules: {
                    Name: { required: true },
                    Type: { required: true }
                },
                messages: {
                    Name: { required: "[名称]不能为空" },
                    Type: { required: "[类型]不能为空" }
                }
            });
            $("#openSelect").click(function () {
                art.dialog.open('DataSelectList.aspx', {
                    id: 'selectDiv', width: 600, height: 400, title: '选择供应商', lock: true,
                    close: function () {
                        var bValue = art.dialog.data('selectValue');
                        if (bValue !== undefined)
                            $("#etst").val(bValue);
                        else
                            $("#etst").val('');
                    }
                }, false);
            })
            //获取配置的colomn字段(暂时手写吧,格式不对)
            colData = [
                { header: '名称', name: 'Name', width: 150, align: 'center' },
                { header: '备注', name: 'Note', width: 250, align: 'center' },
                { header: '类型', name: 'Type', width: 200, align: 'center' },
                { header: '整数型值', name: 'IntValue', width: 60, align: 'center' },
                { header: '浮点型值', name: 'CurrValue', width: 60, align: 'center' },
                { header: '布尔型值', name: 'BoolValue', width: 60, align: 'center', renderer: genRenderCheckbox },
                { header: '字符型值', name: 'StrValue', width: 200, align: 'center' },
                { header: '日期型值', name: 'DateValue', width: 120, align: 'center', renderer: genRenderDateformat },
                { header: '创建人', name: 'UserCreate', width: 100, align: 'center' },
                {
                    header: '创建时间', name: 'CreateDate', width: 200, align: 'center',
                    renderer: genRenderDateformat
                },
                { header: '修改人', name: 'UserModify', width: 100, align: 'center' },
                {
                    header: '修改时间', name: 'ModifyDate', width: 200, align: 'center',
                    renderer: genRenderDateformat
                }
            ];
            defaultSort = { sortby: 'uid', sortdir: 'asc' };
            defaultWhere = { where: ' 1=1 ' };
            listGrid = $('#listgrid').omGrid({
                width: 'auto',
                title: '参数设置列表',
                singleSelect: true,
                //autoFit: true,
                limit: 20,
                extraData: $.extend(defaultWhere, defaultSort),
                onRowClick: function (rowIndex, rowData, event) {
                    currentGIndex = rowIndex;
                    updateFlag = true; addFlag = false;
                    oldRowData = rowData || {};
                    showDetialForm("数据明细", rowData);
                },
                dataSource: '../Handler/grid.ashx?view=vParameter',
                colModel: colData
            });
        });

        genRenderCheckbox = function (colValue, rowData, rowIndex) {
            if (colValue) {
                return '<input type="checkbox" id="' + rowData.OrderNo + '_' + rowData.SeqNo + '" checked="checked" disabled="disabled"></input>';
            }
            return '<input type="checkbox" id="' + rowData.OrderNo + '_' + rowData.SeqNo + '" disabled="disabled"></input>';
        }

        genRenderDateformat = function (colValue, rowData, rowIndex, dateformat) {
            return (colValue.length == 0 || colValue == null) ? '' : new Date(parseInt(colValue.replace("/Date(", "").replace(")/", ""), 10)).Format("yyyy-MM-dd");
        }

        showToolBar = function (form) {
            $('span#button-new', form).omButton({
                icons: { left: '../Content/Images/List/add.png' }, width: 80,
                onClick: function () {
                    if (detailPanel) {
                        addFlag = true; updateFlag = false;
                        controlBtn(form, 'disable', 'disable', 'disable', 'enable', 'enable')
                        $("textarea", detailPanel).val("");
                        $("input[type='text']", detailPanel).each(function (index, element) {
                            $(this).val("");
                            if (index == 1) $(this).focus();
                        });
                        $("input[type='checkbox']", detailPanel).attr('checked', false);
                        $("#UserCreateID", detailForm).val(currentUID);
                        $("#CreateDate", detailForm).val((new Date()).Format("yyyy-MM-dd hh:mm"));
                    }
                }
            });
            $('span#button-copy', form).omButton({
                icons: { left: '../Content/Images/List/copy.png' }, width: 80,
                onClick: function () {
                    if (detailPanel) {
                        addFlag = true; updateFlag = false;
                        controlBtn(form, 'disable', 'disable', 'disable', 'enable', 'enable')
                        $("input[type=text]", detailPanel).eq(0).val("")
                        $("input[type=text]", detailPanel).eq(1).focus()
                        $("#UserCreateID", detailForm).val(currentUID);
                        $("#CreateDate", detailForm).val((new Date()).Format("yyyy-MM-dd hh:mm"));
                    }
                }
            });
            $('span#button-delete', form).omButton({
                icons: { left: '../Content/Images/List/delete.png' }, width: 80,
                onClick: function () {
                    alert('未执行')
                }
            });
            $('span#button-save', form).omButton({
                icons: { left: '../Content/Images/List/accept.png' }, width: 80,
                onClick: function () {
                    if (currentUID == "0") {
                        $.omMessageBox.alert({
                            type: 'warning',
                            title: '友情提醒',
                            content: '您登录已超时,请重新登录继续操作...',
                            onClose: function () {
                                window.top.location.href = decodeURIComponent("../Index.aspx")
                            }
                        });
                    }
                    else {
                        if (!detailForm.data('changed')) {
                            $.omMessageBox.alert({
                                type: 'warning',
                                title: '友情提醒',
                                content: '表单数据没有更改无需保存'
                            });
                        }
                        else {
                            if (validatorForm.form()) {
                                if (addFlag == true && updateFlag == false) {
                                    detailForm.attr("action", actionRoot + "&method=AddParameter");
                                }
                                if (addFlag == false && updateFlag == true) {
                                    detailForm.attr("action", actionRoot + "&method=UpdateParameter");
                                }
                                submitForm(detailForm, function (data) {
                                    if (data.IsError) {
                                        $.omMessageBox.alert({
                                            type: 'error',
                                            content: '发现系统错误<BR>错误码：' + data.Message
                                        });
                                    }
                                    else {
                                        detailPanel.omPanel("close");
                                        if (addFlag == false && updateFlag == true) {
                                            listGrid.omGrid("deleteRow", currentGIndex);
                                        }
                                        listGrid.omGrid("insertRow", 0, data.Data, true);
                                        listGrid.omGrid('saveChanges');
                                        listGrid.omGrid("refresh");
                                        $.omMessageBox.alert({
                                            type: 'success',
                                            content: '数据保存成功'
                                        })
                                    }
                                    updateFlag = true; addFlag = false;
                                    detailForm.data('changed', false);
                                    controlBtn(form, 'enable', 'enable', 'disable', 'disable', 'disable');
                                });
                            }
                            else {
                                $.omMessageBox.alert({
                                    type: 'error',
                                    content: '数据校验有误,请检查'
                                });
                            }
                        }
                    }
                }
            });
            $('span#button-cancel', form).omButton({
                icons: { left: '../Content/Images/List/cross.png' }, width: 80,
                onClick: function () {
                    showDetialForm("数据明细", oldRowData);
                    addFlag = false; updateFlag = true;
                    controlBtn(form, 'enable', 'enable', 'disable', 'disable', 'disable')
                }
            });
        }

        showSearchPanel = function () {
            $('span#button-search').omButton({
                icons: { left: '../Content/Images/List/search.png' }, width: 80,
                onClick: function (event) {
                    var custWhere = '';
                    $("#search-panel input[type=text]").each(function () {
                        if ($(this).val().length > 0) {
                            custWhere += ' AND ' + $(this).attr('id') + ' LIKE ' + '\'%' + $(this).val() + '%\'';
                        }
                    })
                    listGrid.omGrid({ extraData: $.extend({ where: defaultWhere.where + custWhere }, defaultSort) });
                    addFlag = false; updateFlag = true;
                    if (detailPanel) {
                        detailPanel.omPanel("close");
                    }
                }
            });
            $('span#button-clear').omButton({
                icons: { left: '../Content/Images/List/search.png' }, width: 80,
                onClick: function (event) {
                    $("div[id='search-panel'] input").val("");
                }
            });
        };

        showDetialForm = function (title, rowData) {
            if (detailPanel) {
                $("input[type=text]", detailPanel).val("");
                detailPanel.omPanel("open");
            }
            else {
                detailPanel = $('#detail-form').omPanel({
                    width: 'fit',
                    title: title,
                    collapsed: false,
                    collapsible: false,
                    closable: false
                });
            }
            validatorForm.resetForm();
            rowData = rowData || {};
            $.each(rowData, function (n, value) {
                if (n == 'CreateDate' || n == "DateValue")
                    $("input[id='" + n + "']", detailPanel).val(value == null ? '' : new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10)).Format("yyyy-MM-dd hh:mm"));
                else {
                    if ($("input[id='" + n + "']", detailPanel).length == 1) {
                        if (n == 'BoolValue')
                            $("input[id='" + n + "']", detailPanel).attr("checked", value);
                        else
                            $("input[id='" + n + "']", detailPanel).val(value);
                    }
                    else {
                        $("textarea[id='" + n + "']", detailPanel).html(value);
                    }
                }
            })
        };

        controlBtn = function (form, a, b, c, d, e) {
            if (form) {
                $('span#button-new', form).omButton(a)
                $('span#button-copy', form).omButton(b)
                $('span#button-delete', form).omButton(c)
                $('span#button-save', form).omButton(d)
                $('span#button-cancel', form).omButton(e)
            }
        }

        submitForm = function (mainform, success, error) {
            if (!mainform)
                return false;
            else {
                mainform.ajaxSubmit({
                    type: "POST",
                    dataType: 'json',
                    success: success,
                    clearForm: true,
                    beforeSubmit: function (formData, jqForm, options) {
                        //针对复选框和单选框 处理
                        $(":checkbox,:radio", jqForm).each(function () {
                            if (!existInFormData(formData, this.name)) {
                                formData.push({ name: this.name, type: this.type, value: this.checked });
                            }
                        });
                        for (var i = 0, l = formData.length; i < l; i++) {
                            var o = formData[i];
                            if (o.type == "checkbox" || o.type == "radio") {
                                o.value = $("[name=" + o.name + "]", jqForm)[0].checked ? "true" : "false";
                            }
                        }
                    },
                    beforeSend: function (a, b, c) {
                        $.omMessageBox.waiting({
                            title: '正在保存数据中...',
                            content: '服务器正在处理请求......'
                        });
                    },
                    complete: function () {
                        $.omMessageBox.waiting("close")
                    },
                    error: function (result) {
                        $.omMessageBox.waiting("close")
                        $.omMessageBox.alert({
                            content: '发现系统错误 <BR>错误码：' + result.status
                        });
                    }
                });
            }
        }

        function existInFormData(formData, name) {
            for (var i = 0, l = formData.length; i < l; i++) {
                var o = formData[i];
                if (o.name == name) return true;
            }
            return false;
        }

    </script>
</head>
<body>
    <div id="west-panel" class="westpanel">
        <div id="search-panel">
            <table style="top: 0px; height: 100%;">
                <tr>
                    <td style="width: 60px" align="right">
                        <span class="label">名称</span>
                    </td>
                    <td>
                        <input type="text" class="input-text" id="Name" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 60px" align="right">
                        <span class="label">说明</span>
                    </td>
                    <td>
                        <input type="text" class="input-text" id="Note" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="center-panel">
        <div id="detail-form" style="display: none;">
            <form id="viewForm">
                <table>
                    <tr style="display: none;">
                        <td colspan="4">
                            <input name="UID" id="UID" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px">名&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称：</td>
                        <td align="left" style="width: 150px">
                            <input type="text" class="input-text" name="Name" id="Name" style="width: 100%;" /></td>
                        <td align="right" style="width: 150px">类&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;型：</td>
                        <td align="left" style="width: 150px">
                            <input type="text" class="input-text" name="Type" id="Type" value="0" style="width: 100%;" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px; color: red;">说&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;明：</td>
                        <td colspan="3">
                            <textarea cols="50" rows="3" name="Note" id="Note" style="width: 100%;"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px">字符型值：</td>
                        <td colspan="3">
                            <input type="text" class="input-text" name="StrValue" id="StrValue" style="width: 100%;" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px">浮点型值：</td>
                        <td align="left" style="width: 150px">
                            <input type="text" class="input-text" name="CurrValue" id="CurrValue" style="width: 100%;" /></td>
                        <td align="right" style="width: 150px">整数型值：</td>
                        <td align="left" style="width: 150px">
                            <input type="text" class="input-text" name="IntValue" id="IntValue" value="0" style="width: 100%;" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px">布尔型值：</td>
                        <td align="left" style="width: 150px">
                            <input type="checkbox" class="input-text" name="BoolValue" id="BoolValue" style="width: 100%;" /></td>
                        <td align="right" style="width: 150px">日期型值：</td>
                        <td align="left" style="width: 150px">
                            <input type="text" class="input-text" name="DateValue" id="DateValue" style="width: 100%;" />
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 50px">布尔型值：</td>
                        <td colspan="3">
                            <div class="sw_b">
                                <input name="etst" type="text" id="etst" /><input id="openSelect" class="sw_sb sw_qbtn" type="button" />
                            </div>
                        </td>
                    </tr>
                    <tr style="display: none;">
                        <td colspan="4">
                            <span class="label">创建人：</span>
                            <input type="text" class="input-text" name="UserCreateID" id="UserCreateID" />
                            <span class="label">创建日期：</span>
                            <input type="text" class="input-text" name="CreateDate" id="CreateDate" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left" colspan="4">
                            <span id="button-new">新增</span>
                            <span id="button-copy">复制</span>
                            <span id="button-delete">删除</span>
                            <span id="button-save">保存</span>
                            <span id="button-cancel">取消</span>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <table id="listgrid"></table>
    </div>
    <div id="south-panel">
        <table>
            <tr>
                <td>
                    <span id="button-search">搜索</span>
                    <span id="button-clear">清空</span>
                    <span id="button-load">加载</span>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>
